Skip to main content

Full text of "Arduino"

See other formats

Arduino in C: See Your Microcontroller in a New Way 


Make Projects 

build, hack, tweak, share, discover, J 

Arduino in C: See Your 

Microcontroller in a New Way 

Written By: Chandler 

*/* TOOLS: 

• Computer running Linux (1) 

• USB A to B cabled) 


Arduino microcontroller (1) 


For this project, you need to have a Linux computer, or else the things I describe will not 
work. Don't fret, though, because there are plenty of tutorials out there that will help you. 
Piecing this together from multiple sources has been hard, but I've been able to do it on my 
Arduino Duemilanove and Arduino clone. Finally, I would like to say thanks to the following 
sources, because they helped me the most and may help you. All of my code and setup was 
based on these tutorials! Thank you! . . . . .. .. 

© Make Projects 

Page 1 of 6 

Arduino in C: See Your Microcontroller in a New Way 

Step 1 — Arduino in C: See Your Microcontroller in a New Way 

wat5onc@UbuntuCompl71:~$ sudo apt-get install gcc-avr 

[sudo] password for watsonc: 

Reading package lists... Done 

Building dependency tree 

Reading state information... Done 

gcc-avr is already the newest version. 

The following packages were automatically installed and are no longer required 
libcommons-collections3-java libswing-layout-java jsvc junit4 liboro-java 
libasm3-java libnb-java4-java libnb-platform-devel-java default-jdk-doc 
libnb-apisupport2 Java dvipng junit libf reemarker-java 
libcommons-compress-java libregexp-java texlive-common liblog4j 1.2-java 
javahelp2 libservlet2.4-java liblucene2-java jetty libslf4j -Java 
libbindex-java jrubyl.l libnb-idel3-java libdb4.7-java-gcj 
libcommons-logging-java openjdk-6-doc libequinox-osgi-java libcobertura-java 
texlive-binaries libfelix-main-java libini4j -Java libjetty-java 
libcommons-net-java libasm2-java libcommons-daemon-java liblzo2-2 
libcommons-beanutils-java libdb-je-java tex4ht-common libswingworker-java 
libdb4.7-java libcommons-digester-java libhamcrest-java libjtidy-java 
libjzlib-java libfelix-f ramework-java libxml-commons-resolverl.l-java 
libswingx-java libappf ramework-java libicu4j -Java libservlet2.3-java 
libjsch-java libnb-javaparser-java libnb-svnclientadapter-java tex-common 
libbeansbinding-java libnb-platforml2-java 

Use "apt-get autoremove" to remove them. 

upgraded, 9 newly installed, to remove and 50 not upgraded. 


After you have all of your parts 
(note that the USB cable can be 
replaced with another programmer, 
as long as you can program your 
Arduino or Arduino clone), you can 
begin installing avr-gcc (the C 
compiler for the Arduino). On 
Ubuntu, in a terminal window type: 

• sudo apt-get install 

Enter your root password, and that 
should do it. 

For other distrubutions of Linux, 
install the gcc-avr package in the 
same way that you would another 

= Back ' /Forward ▼ ^ ^ .^ ( ^ 100% Q | Icon View 




Once that's done, you can begin. 
Create a new folder on your 
desktop called "AVR- 
GCC_FORMAT" or something like 
that. Open it and read all of the 
instructions. Once done with that, 
you can move on. 

© Make Projects 

Page 2 of 6 

Arduino in C: See Your Microcontroller in a New Way 

a 1 ^^ ! P en T J^Save ta£ <i|\Llrvdo 

"w-u inituui 



= at90s2333 


= at 99544 14 


= at 9954433 


= at9&s4434 


= at9>&sfl515 


= at9&s£53S 


= atme^al28 


= atfflegal286 


= atme<jai2ei 


= atm&g.ai2S4p 


= atmeg-alG 


= atmegal63 


= atmegaHJ4p 


= atmtgaies 


= atmegaiea 1 

■ ■ 




= atmega256Q 


= at mega 2 561 


= atme-ga.32 


= atmega324p 


= atfflSg3325 


= atmega32bQ 


= atmega329 


= atmega329e 


= atmegs48 


= atm^ga^4 

Q, 9t 


Forward T X © C 

j| f Q, ioo% ©, 

Icon View 


i M watsonc 

M Desktop new avr-g« file 








• Now that you've done that, let's talk about what just happened. The Makefile is what 
compiles the "file.c" file and dispenses a .hex file, along with a bunch of other stuff. To set 
the makefile to your Arduino, you might have to tweak it a bit. If your Arduino is running on 
an ATmega328P, you're fine and can move on. If you are running any other chip (such as 
an ATTiny or another version of the ATmega), you will have to change the MCU_TARGET 
variable in the Makefile. Simply go into it, and place a # before the line: 

• MCU_TARGET = atmega328p 

• The # denotes a comment, so you're "commenting out" that line of code. Then, find the 
chip that's yours (say it's an ATmega168) and remove the # before that line. Save the file, 
and you're done. 

• Now, you can rename file.c if you want to. This is quite a dull name, after all. I'd 
recommend flashlight . c, but anything works if it ends in ".c" (and follows a bunch of 
weird restrictions that you must be aware of). I'd strongly recommend just to go with 

flashlight . c. 

• After changing that, you have to change the Makefile's first two lines to: 

• PRG = flashlight 

• OBJ = flashlight. o 

• so that the Makefile knows what it's compiling. 

© Make Projects 

Page 3 of 6 

Arduino in C: See Your Microcontroller in a New Way 

© Make Projects Page 4 of 6 

Arduino in C: See Your Microcontroller in a New Way 

vatsonc@UbuntuCompl71:~/Desktop/Flashlight$ make 

ci.'-yLL -y -LTdLL -uz -n::L'j=dLr:eydJ^ap -l -u i L^shlight.o flashlight. c 

avr-gcc -g -Wall -02 -mmcu=atmega328p -Wl, -Map, flashlight. map -o flashlight. elf 
flashlight. o 
avr-objdump -h -S flashlight. elf > flashlight. 1st 
avr-objcopy -j .text -j .data -0 ihex flashlight. elf flashlight. hex 
avr-objcopy -j .text -j .data -0 binary flashlight. elf flashlight.bin 
avr-objcopy -j .text -j .data -0 srec flashlight. elf flashlight. srec 
avr-objcopy -j .eeprom -change-section-lma .eeprom=0 -0 ihex flashlight. elf fla 
shlighteeprom.hex \ 

|| { echo empty flashlighteeprom.hex not generated; exit @; } 
avr-objcopy: --change-section-lma . eeprom=0x00000000 never used 
avr-objcopy -j .eeprom --change-section-lma .eeprom=0 -0 binary flashlight. elf f 
lashlight_eeprom.bin \ 

|| { echo empty flashlight_eeprom.bin not generated; exit 0; } 
avr-objcopy: --change-section-lma .eeprom=0x00000000 never used 
avr-objcopy -j .eeprom -change-section-lma .eeprom=0 -0 srec flashlight. elf fla 
shlighteeprom.srec \ 

|| { echo empty flashlighteeprom.srec not generated; exit 0; } 
avr-objcopy: --change-section-lma .eeprom=0x00000000 never used 
watsonc@UbuntuCompl71:-/Desktop/Flashlight$ avrdude -p m328p -P /dev/ttyUSBO -c 
stk500vl -F -u -U flash:w:flashlight.hex 
\avrdude: ser open(): can't open device "/dev/ttyUSB0" : No such file or director 

5tK50flvl -F -u -U flash:w:flashlight.nex 

it$ avrdude -p m328p -P /dev/ttyUSBe -c 

avrdude: AVR device initialized and ready to accept instructions 

Reading | ################################################## | 100% 0.01s 

avrdude: Device signature = 0x000000 

avrdude: Yikes! Invalid device signature. 

avrdude: Expected signature for ATMEGA328P is IE 95 BF 

avrdude: NOTE: FLASH memory has been specified, an erase cycle will be performed 

• Now let's get to compiling and 

• First, make a copy on the desktop 
of your AVR-GCC_FORMAT folder. 
Then, open a terminal window and 
cd to that folder. Type the 

command make and the makefile 

will run. A bunch of files will 
appear. This is why I 
recommended that you do this 
inside a folder rather than on your 
desktop or in your home folder. 

• Now, once you're done with that, 
plug in your Arduino and type the 
following in the terminal window 
(make sure to reset your Arduino 
just before entering the command): 

• avrdude -p version -P 
location -c stk500vl -u 
-U -F 
flash : w : filename . hex 

• Make sure you replace version with 
the name of your chip (m328p for 
ATmega328P, m168for 
ATmega168, or type in something 
random to cause an error and you'll 
get the rest of the list). 

• location should be replaced with 
something like /dev/ttyUSBO if 
that's the location of your Arduino 
(if you're not sure, and it's plugged 
into a USB port, try that first). 

• Finally, filename.hex should be 
replaced with the filename, which in 

this case is flashlight, hex. 

© Make Projects 

Page 5 of 6 

Arduino in C: See Your Microcontroller in a New Way 

For example, say you were 
downloading flashlight. hex onto an 
ATMega328P at /dev/ttyUSBO. 

You'd type: avrdude -p m328p 
-P /dev/ttyUSBO -c 
stk500vl -u -U -F 
f lash: w: flashlight . hex 

• All the programming is done! 

• Simply connect an LED from pin 13 to GND, connect a pull-down resistor (10k) from pin 4 
to GND, and connect a wire to GND. Touch the other end of the wire to the part of the 
resistor closest to pin 4, and the light will turn on. Do it again, and it will turn off. You have 
completed your first project programming an Arduino in pure C! 

This document was last generated on 201 2-1 1-011 2:1 2:56 PM. 

© Make Projects 

Page 6 of 6